//道路挖掘
function selectDlwjQyxx(tplcode) {
  ExtElWin({id: 'selectQyxxWin', title: '选择申请企业', w: 550, h: 400, tplcode: tplcode});
}

function ExtElWin(f) {
  var w = 350;
  var h = 160;
  if (f.w) w = f.w;
  if (f.h) h = f.h;
  var qyxxGrid = new Ext.ux.grid.YwGrid({
    region: 'center',
    layout: 'fit',
    tplcode: f.tplcode,
    margins: '1 1 1 1',
    needSelect4CsdlwjTbar:true,
    url: '/yw-com-qyxx!dataList.action?templateCode='+f.tplcode,
    keyField: 'id',
    defaultSortField: 'sqrq',
    structure: [{
      name: 'id',
      hidden: true
    },{
      name: 'post',
      hidden: true
    },{
      name: 'qyjc',
      hidden: true
    },{
      name: 'qyphone',
      hidden: true
    },{
      name: 'cz',
      hidden: true
    },{
      name: 'fddbr',
      hidden: true
    },{
      name: 'frphone',
      hidden: true
    },{
      name: 'frtel',
      hidden: true
    },{
      name: 'gszcdjh',
      hidden: true
    },{
      name: 'qydlyszh',
      hidden: true
    },{
      name: 'gszcdjyxq',
      hidden: true
    },{
      name: 'qydlyszyxq',
      hidden: true
    },{
      name: 'ywlxr',
      hidden: true
    },{
      name: 'ywlxphone',
      hidden: true
    },{
      name: 'ywlxrtel',
      hidden: true
    },{
      name: 'address',
      hidden: true
    },{
      name: 'email',
      hidden: true
    },{
      name: 'zczj',
      hidden: true
    },{
      name: 'csmj',
      hidden: true
    },{
      name: 'frzw',
      hidden: true
    },{
      name: 'zzdj',
      hidden: true
    },{
      name: 'zsbh',
      hidden: true
    },{
      name: 'jjlx',
      hidden: true
    },{
      name: 'qyRemarks',
      hidden: true
    },{
      name: 'rsJsry',
      hidden: true
    },{
      name: 'rsWxg',
      hidden: true
    },{
      name: 'rsTotal',
      hidden: true
    },{
      name: 'dwslJyrq',
      hidden: true
    },{
      name: 'dwslGyrq',
      hidden: true
    },{
      name: 'qylx',
      hidden: true
    },{
      name: 'sqdw',
      header: '企业名称'
    },{
      name: 'sqrq',
      header: '申请日期',
      width: 12
    }]
  });
  
  var store = qyxxGrid.store;
  var toolbar = qyxxGrid.toolbar;
  store.on('beforeload', function(){
    store.baseParams = {
      sqdw: toolbar.search_sqdw.getValue()
    };
  }, this);
  
  new Ext.Window({
    id        : f.id,
    title     : f.title,
    width     : w,
    height    : h,
    iconCls   : 'icon_title',
    layout    : 'fit',
    modal     : true,
    plain     : true,
    resizeable: false,
    items     : [qyxxGrid]
  }).show();
}

function selectDlwjCallback(params) {
	try{
	    setObjvalue(document.forms[0].shlj_cz_qymc,params.r.get('sqdw'));
	    setObjvalue(document.forms[0].shlj_cz_frdb,params.r.get('fddbr'));
	    setObjvalue(document.forms[0].shlj_cz_lxdh,params.r.get('qyphone'));
	    setObjvalue(document.forms[0].shlj_cz_email,params.r.get('email'));
	    setObjvalue(document.forms[0].shlj_cz_qybh,params.r.get('id'));
	    setObjvalue(document.forms[0].shlj_cz_bgzs,params.r.get('address'));
	    setObjvalue(document.forms[0].shlj_cz_cz,params.r.get('zczj'));
	    setObjvalue(document.forms[0].shlj_cz_yzbm,params.r.get('post'));
	    setObjvalue(document.forms[0].shlj_cz_zczb,params.r.get('zczj'));
	}catch(e){}
}

function setObjvalue(obj,value){
    if(obj && value)
	   obj.value = value;
	else if(obj)
	   obj.value="";
}

//给tabPanel添加事件，tab变换的时候，
//触发事件
function addEventListenerToTab(){
	var tabPanel = parent.Ext.getCmp('centerPanel');
	curTab = tabPanel.getActiveTab(); //当前活动的tab
	if(tabPanel){
		if(!tabPanel.hasListener('tabchange'))
			tabPanel.addListener('tabchange',changeEvent);
	}
}

//重写setTimeout方法，以使能够传递参数
//IE下不能传递参数
function setTimeoutForIE(pFunction, iTime, params){
     var fuckIE = function(){
             pFunction(params);    
      }
      var pTimeout = setTimeout(fuckIE, iTime);
      return pTimeout;
}

var preTab; //前一个活动的tab
var curTab;//当前活动的tab
 
var firstIn2fb =true;// 第一次进入呈批表
//事件的响应
function changeEvent(obj){
	var item = obj.getActiveTab();
	//交换，获得当前和上一个活动的tab
	preTab = curTab;  
	curTab = item;
	var id = item.id;
	if(id == 'shljczcpb_form'){ //转向呈批表
		var main = parent.document.getElementById('shljcz_formIframe').contentWindow.document;
		var field = main.getElementsByName('id')[0];
		if(main && field && field.value !=''){
			item.setDisabled(false);
			if(firstIn2fb){
				setTimeoutForIE(copyElement2Cpb,1500,item);
			}
			firstIn2fb =false;
		}
		else{
			parent.Ext.getCmp('centerPanel').setActiveTab(0);
			item.setDisabled(true);
			Ext.Msg.alert('系统提示','请先保存基本信息表单，再进行呈批表的查看！');
		}
	}else if (id == 'csshlj_sjcl'){ //转向收件材料
		var main = parent.document.getElementById('shljcz_formIframe').contentWindow.document;
		var field = main.getElementsByName('id')[0];
		if(main){
			if(main.getElementsByName('shlj_cz_qymc')[0] && field){
				var QyId = main.getElementsByName('shlj_cz_qymc')[0].value; 
				var AjId = main.getElementsByName('id')[0].value; 
				item.setDisabled(false);
				changeUrlAndReload(item,AjId);
			}else{
				parent.Ext.getCmp('centerPanel').setActiveTab(0);
				item.setDisabled(true);
				Ext.Msg.alert('系统提示','请先保存基本信息表单！');
			}
		}
	}else if(id == 'gzsrxdggcswjscb_form'){ //转向审查表
		var main = parent.document.getElementById('shljcz_formIframe').contentWindow.document;
		var field = main.getElementsByName('id')[0];
		if(main && field){
			
			setTimeoutForIE(copyElements2Scb,1500,item); //设置一定时间的延迟，让页面加载完成
		}else{
			parent.Ext.getCmp('centerPanel').setActiveTab(0);
			item.setDisabled(true);
			Ext.Msg.alert('系统提示','请先保存基本信息表单，再进行审查表的查看！');
		}
	}else if(id == 'shljcz_GIS'){  //转向webgis
		var main = parent.document.getElementById('shljcz_formIframe').contentWindow.document;
		var objectId = main.getElementsByName('id')[0];
		if(main && objectId){
			item.setDisabled(false);
			setTimeout(copy2Gis,1000);
		}else{
			item.setDisabled(true);
		}
	}else if(id == 'shlj_form'){
		var disabledTab = parent.Ext.getCmp('gzsrxdggcswjsqb_form');
		if((preTab.id == 'gzsrxdggcswjsqb_form') && disabledTab.disabled == false)
			copyBz();
	}
}



//复制备注内容
function copyBz(){
	var doc = parent.document.getElementById('gzsrxdggcswjsqb_formIframe').contentWindow.document;
	var scb = parent.document.getElementById('gzsrxdggcswjscb_formIframe').contentWindow.document;
	if(doc && scb){
		var bzhidden = doc.getElementsByName('SR_DLWJSQB_BZ')[0];
		var bzcontent = scb.getElementsByName('SR_DLWJSCB_BZ')[0];
		if(bzhidden && bzcontent)
			bzhidden.value = bzcontent.value;
	}
}

//针对webgis页面
function copy2Gis(){
	var main = parent.document.getElementById('shljcz_formIframe').contentWindow.document;
	var gis = parent.document.getElementById('shljcz_GISIframe').contentWindow.document;
	var acceptNo = main.getElementsByName('SHLJ_CZ_ACCEPT_NO')[0].value;
	var templateCode = WF_CONFIG.templateCode;
	var id = main.getElementsByName('id')[0].value;
	if(gis){
		var src = ctx+'/gzcgwsp/csdlwj/flex.jsp?acceptNo='+acceptNo+'&templateCode='+templateCode+'&id='+id ;
		//parent.Ext.getDom("SHOW_GISIframe").src = src; //此种方法也行
		parent.Ext.getDom("shljcz_GISIframe").contentWindow.location.href = src;
  	}
}

//复制数据到审查表中去
function copyElements2Scb(item){
	var main = parent.document.getElementById('gzsrxdggcswjsqb_formIframe').contentWindow.document;
	var scb = parent.document.getElementById('gzsrxdggcswjscb_formIframe').contentWindow.document;
	if(main && scb){
		item.setDisabled(false);
		if(scb.getElementsByName('SR_DLWJSCB_BH') && main.getElementsByName('SR_DLWJSQB_ACCEPT_NO'))
			scb.getElementsByName('SR_DLWJSCB_BH')[0].value = main.getElementsByName('SR_DLWJSQB_ACCEPT_NO')[0].value;
		if(scb.getElementsByName('SR_DLWJSCB_SQR')[0] && main.getElementsByName('SR_DLWJSQB_JSDW'))
			scb.getElementsByName('SR_DLWJSCB_SQR')[0].value = main.getElementsByName('SR_DLWJSQB_JSDW')[0].value;
		if(scb.getElementsByName('SR_DLWJSCB_XZXKZMC'))
			scb.getElementsByName('SR_DLWJSCB_XZXKZMC')[0].value = '城市道路挖掘申请';
		if(scb.getElementsByName('SR_DLWJSCB_BZ') && main.getElementsByName('SR_DLWJSQB_BZ'))
			scb.getElementsByName('SR_DLWJSCB_BZ')[0].value = main.getElementsByName('SR_DLWJSQB_BZ')[0].value;
	}else{
		parent.Ext.getCmp('centerPanel').setActiveTab(0);
		item.setDisabled(true);
		Ext.Msg.alert('系统提示','打开审查表出错了！');
	}
}



//从基本表单中复制相关的信息到呈批表中
function copyElement2Cpb(item){
	var sqb = parent.document.getElementById('shljcz_formIframe').contentWindow.document;
	var cpb = parent.document.getElementById('shljczcpb_formIframe').contentWindow.document;
	var id = sqb.getElementsByName('id')[0].value;
	if(id != null && id != ''){
		item.setDisabled(false);
		if(sqb && cpb){
			
			//发证时间
			if(cpb.getElementsByName('shljczcpb_fzsj')[0] && sqb.getElementsByName('shlj_cz_fzsjstr')[0] ){
				cpb.getElementsByName('shljczcpb_fzsj')[0].value = changeDateToCn(sqb.getElementsByName('shlj_cz_fzsjstr')[0].value);
			}
			//发证号
			if(cpb.getElementsByName('shljczcpb_fzh')[0] && sqb.getElementsByName('shlj_cz_fzh')[0] ){
				cpb.getElementsByName('shljczcpb_fzh')[0].value = sqb.getElementsByName('shlj_cz_fzh')[0].value;
			}
			//批准经营开始时间
			if(cpb.getElementsByName('shljczcpb_kssj')[0] && sqb.getElementsByName('shlj_cz_jysjsstr')[0]){
				cpb.getElementsByName('shljczcpb_kssj')[0].value = changeDateToCn(sqb.getElementsByName('shlj_cz_jysjsstr')[0].value);
			}
			//批准经营结束时间
			if(cpb.getElementsByName('shljczcpb_jzsj')[0] && sqb.getElementsByName('shlj_cz_jysjestr')[0] ){
				cpb.getElementsByName('shljczcpb_jzsj')[0].value = changeDateToCn(sqb.getElementsByName('shlj_cz_jysjestr')[0].value);
			}
		}
	}else{
		parent.Ext.getCmp('centerPanel').setActiveTab(0);
		item.setDisabled(true);
		Ext.Msg.alert('系统提示','请先保存基本信息表，后再查看呈批表！');
	}
}


//从呈批表中复制相关的信息到基本表单中
function copyCpb2Sqb(){
	if(parent.document.getElementById('shljcz_formIframe') && parent.document.getElementById('shljczcpb_formIframe')){
		var sqb = parent.document.getElementById('shljcz_formIframe').contentWindow.document;
		var cpb = parent.document.getElementById('shljczcpb_formIframe').contentWindow.document;
		var id = sqb.getElementsByName('id')[0].value;
		if(id != null && id != ''){
			if(sqb && cpb){
				//发证时间
				if(cpb.getElementsByName('shljczcpb_fzsj')[0] && sqb.getElementsByName('shlj_cz_fzsjstr')[0]){
					sqb.getElementsByName('shlj_cz_fzsjstr')[0].value = changeDate(cpb.getElementsByName('shljczcpb_fzsj')[0].value);
				}
				//发证号
				if(cpb.getElementsByName('shljczcpb_fzh')[0] && sqb.getElementsByName('shlj_cz_fzh')[0]){
					sqb.getElementsByName('shlj_cz_fzh')[0].value = cpb.getElementsByName('shljczcpb_fzh')[0].value;
				}
				//批准经营开始时间
				if(cpb.getElementsByName('shljczcpb_kssj')[0] && sqb.getElementsByName('shlj_cz_jysjsstr')[0]){
					sqb.getElementsByName('shlj_cz_jysjsstr')[0].value = changeDate(cpb.getElementsByName('shljczcpb_kssj')[0].value);
				}
				//批准经营结束时间
				if(cpb.getElementsByName('shljczcpb_jzsj')[0] && sqb.getElementsByName('shlj_cz_jysjestr')[0]){
					sqb.getElementsByName('shlj_cz_jysjestr')[0].value = changeDate(cpb.getElementsByName('shljczcpb_jzsj')[0].value);
				}
			}
		}
	}
}


function changeDate(date){
	if(date !=null && date.length>0){
		var year = date.substring(0,date.indexOf("年"));
		var month = date.substring(date.indexOf("年")+1,date.indexOf("月"));
		var day = date.substring(date.indexOf("月")+1,date.indexOf("日"));
		return year+"-"+month+"-"+day;
	}else{
		return date;
	}
}

function changeDateToCn(date){
	if(date !=null && date.length>0){
		var year = date.substring(0,date.indexOf("-"));
		var month = date.substring(date.indexOf("-")+1,date.lastIndexOf("-"));
		var day = date.substring(date.lastIndexOf("-")+1);
		return year+"年"+month+"月"+day+"日";
	}else{
		return date;
	}
}
//根据模板编号、案件编号、企业编号
//对收件材料表单重新定向
function changeUrlAndReload(item,AjId){
	var isupdate="";//环节名称对文书的操作参数
    if(WF_CONFIG.activityName!="fqwzxjbr" && WF_CONFIG.activityName!="cksa"){
    	isupdate="false";
    }
	if(!Ext.isEmpty(AjId)){
		item.setDisabled(false);
		if(item){
			var src = 'yw-com-sjcllist!ywList.action?tplcode=shljcz&ajid='+AjId+'&isUpdate='+isupdate;
			parent.Ext.getDom("csshlj_sjclIframe").src = src;
			//parent.Ext.getDom("csdlwj_sjclIframe").contentWindow.location.href = src;
		}
	}else{
		parent.Ext.getCmp('centerPanel').setActiveTab(0);
		item.setDisabled(true);
		Ext.Msg.alert('系统提示','请先保存基本信息表！');
		//item.setDisabled(true);
		//Ext.Msg.alert('系统提示','请先保存基本信息表，后再查看收件材料表！');
	}
}

//验证提交的表单
function checkElementBeforeSubmit(){
	var obj;
	var sqdw = document.getElementsByName('shlj_cz_qymc')[0];
	var frdb = document.getElementsByName('shlj_cz_frdb')[0];
	var lxdh = document.getElementsByName('shlj_cz_lxdh')[0];
	
	if(sqdw.value == ''){
		obj = {
			result:false,
			text:'系统提示：企业名称不能为空！',
			target:sqdw
		};
		return obj;
	}
	if(frdb.value == ''){
		obj = {
			result:false,
			text:'系统提示：法人代表不能为空！',
			target:frdb
		};
		return obj;
	}
	if(lxdh.value == ''){
		obj = {
			result:false,
			text:'系统提示：联系电话不能为空！',
			target:lxdh
		};
		return obj;
	}
	obj = {
		result:true,
		text:'验证通过',
		target:null
	};
	return obj;
}

//验证收件材料
function myValidate(id){
	var sjcl = parent.document.getElementById(id).contentWindow;
	var _sjclList = sjcl.document.getElementById('sjcl_list');
	var obj = null;
	if(_sjclList != null)
	{
		for(var i=1;i<_sjclList.rows.length;i++)
		{
			var _childNodes=_sjclList.rows[i].cells;
			if(_sjclList.rows[i].hasExists == 'false')
			{
			    var alertText =  '*'+_childNodes[1].innerHTML+'\u6CA1\u6709\u4E0A\u4F20,\u8BF7\u5148\u4E0A\u4F20\u8BE5\u9644\u4EF6!';
				obj = {text:alertText,result:false};
				return obj;
			}
		}
		obj = {text:null,result:true};
	}
    return obj;
}


function doUpdaefym(){
	if(parent.document.getElementById('shljczcpb_formIframe') && parent.document.getElementById('shljcz_formIframe')){
		var cpb = parent.document.getElementById('shljczcpb_formIframe').contentWindow.document;
		var sqb = parent.document.getElementById('shljcz_formIframe').contentWindow.document;
		var id = sqb.getElementsByName('id')[0].value;
		if(cpb && sqb && id != null && id != ''){
				var fzsj ="";
				var fzh ="";
				var jysjs ="";
				var jysje ="";
				var xmqy ="";
				
				//发证时间
				if(cpb.getElementsByName('shljczcpb_fzsj')[0] ){
					fzsj =changeDate(cpb.getElementsByName('shljczcpb_fzsj')[0].value) ;
				}
				//发证号
				if(cpb.getElementsByName('shljczcpb_fzh')[0] ){
					fzh = cpb.getElementsByName('shljczcpb_fzh')[0].value ;
				}
				//批准经营开始时间
				if(cpb.getElementsByName('shljczcpb_kssj')[0]){
					jysjs = changeDate(cpb.getElementsByName('shljczcpb_kssj')[0].value) ;
				}
				//批准经营结束时间
				if(cpb.getElementsByName('shljczcpb_jzsj')[0] ){
					jysje = changeDate(cpb.getElementsByName('shljczcpb_jzsj')[0].value) ;
				}
				//项目区域
				if(cpb.getElementsByName('shljczcpb_qy')[0] ){
					xmqy = cpb.getElementsByName('shljczcpb_qy')[0].value ;
				}
				var url="${ctx}/csshlj-cz!doUpdateFym.action?fid="+id+"&fzsj="+fzsj+"&fzh="+fzh+"&xmqy="+xmqy+"&jysjs="+jysjs+"&jysje="+jysje;
				/*$.ajax({
					url : url,
					type : "post",
					dataType : "html"
				});*/
		}
	}		
}

